package gui;

import java.awt.Color;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import kernel.FiberSet;

public class FiberSetSetup {
	private String filepath;
	private Color color;
	private int renderCount;
	private int maximumRenderCount;
	private boolean[] renderType;
	private boolean loaded;
	
	public FiberSetSetup()
	{
		loaded = false;
		color = Color.GRAY;
	}
	
	public static boolean[] getRenderType(String renderTypeRaw)
	{
		boolean[] renderType = new boolean[4];
		if(renderTypeRaw.contains(FiberSet.LINES))
		{
			renderType[0] = true;
		}
		if(renderTypeRaw.contains(FiberSet.POINTS))
		{
			renderType[1] = true;
		}
		if(renderTypeRaw.contains(FiberSet.START_POINTS))
		{
			renderType[2] = true;
		}
		if(renderTypeRaw.contains(FiberSet.END_POINTS))
		{
			renderType[3] = true;
		}
		return renderType;
	}

	public FiberSetSetup(String filepath, Color color, int renderCount,
			boolean[] renderType) {
		this();
		this.filepath = filepath;
		this.color = color;
		this.renderCount = renderCount;
		this.renderType = renderType;
		scanFileForFiberCount();
	}

	private void scanFileForFiberCount()
	{
		try {
			File file = new File(filepath);
			BufferedReader in = new BufferedReader(new FileReader(file));
			String line = "";
			while((line = in.readLine()) != null)
			{
				if(line.contains("LINES"))
				{
					String[] info = line.split("\\s");
					maximumRenderCount = Integer.parseInt(info[1]);
					if(renderCount == 0)
					{
						renderCount = maximumRenderCount;
					}
					break;
				}
			}
			in.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public String getFilepath() {
		return filepath;
	}

	public void setFilepath(String filepath) {
		this.filepath = filepath;
	}

	public Color getColor() {
		return color;
	}

	public void setColor(Color color) {
		this.color = color;
	}

	public int getRenderCount() {
		return renderCount;
	}
	
	public int getMaximumRenderCount(){
		return maximumRenderCount;
	}

	public void setRenderCount(int renderCount) {
		this.renderCount = renderCount;
	}

	public boolean[] getRenderType() {
		return renderType;
	}

	public void setRenderType(boolean[] renderType) {
		this.renderType = renderType;
	}
	
	public String getName()
	{
		int lastIndex = filepath.lastIndexOf("/");
		if(lastIndex < 0)
		{
			lastIndex = filepath.lastIndexOf("\\");
		}
		return filepath.substring(lastIndex);
	}
	public void setLoaded()
	{
		loaded = true;
	}
	
	public boolean isLoaded()
	{
		return loaded;
	}
	
	public boolean equals(Object object)
	{
		FiberSetSetup newFss = (FiberSetSetup)object;
		return filepath.equals(newFss.getFilepath());
	}

}
